package org.ws.lifebacklog.view;

import org.ws.lifebacklog.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Navigation bar component. <code>
 * Ensure that navigation_bar.xml is present  into the folder layout.
 * Add follow text into your layout.xml file.
  	<code><org.jazzteam.odin.view.NavigationBarView
			android:id="@+id/navigation_bar"
			android:layout_height="wrap_content"
			android:layout_width="fill_parent" /></code>
 * 
 * 
 * use follow for update nav bar <code>((NavigationBarView) findViewById(R.id.navigation_bar)).update(R.string.profile,
 * R.string.menu, defaultFinishEvent, null, null);</code>
 * 
 * if you button will be show only if have valid label id and callback function.
 * 
 * @author sergey.valuy
 * */
public class NavigationBarView extends RelativeLayout {
	private View view;
	private Button btnBack;
	private TextView title;
	private Button btnNext;

	public NavigationBarView(Context context) {
		super(context);
		init(context);
	}

	public NavigationBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public NavigationBarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		view = LayoutInflater.from(context).inflate(R.layout.view_navigation_bar, this);
		btnBack = (Button) view.findViewById(R.id.nav_back);
		btnNext = (Button) view.findViewById(R.id.nav_next);
		title = (TextView) view.findViewById(R.id.nav_title);
	}

	public void update(CharSequence title, Integer leftButtonTextId, final OnClickListener leftButtonOnClickListener,
			Integer rightButtonTextId, final OnClickListener rightButtonOnClickListener) {
		update(title, getString(leftButtonTextId), leftButtonOnClickListener, getString(rightButtonTextId),
				rightButtonOnClickListener);
	}

	public void update(CharSequence title, CharSequence leftButtonName,
			final OnClickListener leftButtonOnClickListener, CharSequence rightButtonName,
			final OnClickListener rightButtonOnClickListener) {

		this.title.setText(title);
		this.btnNext.setText(rightButtonName);

		if (leftButtonName != null && leftButtonOnClickListener != null) {
			btnBack.setText(leftButtonName);
			btnBack.setVisibility(VISIBLE);
			btnBack.setOnClickListener(leftButtonOnClickListener);
		} else {
			btnBack.setVisibility(INVISIBLE);
		}

		if (rightButtonName != null && rightButtonOnClickListener != null) {
			btnNext.setText(rightButtonName);
			btnNext.setVisibility(VISIBLE);
			btnNext.setOnClickListener(rightButtonOnClickListener);
		} else {
			btnNext.setVisibility(INVISIBLE);
		}
	}

	private String getString(Integer resId) {
		return resId == null ? null : getResources().getString(resId);
	}
}
